package com.boge.flow;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class FlowableTest2 {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/01-base/FirstFlow.bpmn20.xml")
                .name("第1个流程图")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
    }

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow2() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/01-base/FirstFlow.bpmn20.xml")
                .name("第2个流程图")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
    }

    @Test
    void startFlow() {
        //流程定义表中动态维护的 act_re_procdef=>id
        String processDefinitionId = "FirstFlow:1:d2b617dd-834b-11f0-95a1-ae3fe006c356";
        //我们创建流程图的时候自定义的。注意保证唯一  act_re_procdef=>key
        String processKey = "FirstFlow";
        //根据流程定义id启动流程(推荐)
        //ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId);
        //根据流程定义key启动流程
        runtimeService.startProcessInstanceByKey(processKey);
    }

    /**
     * 根据用户查询待办信息
     */
    @Test
    void finishFlow() {
        //获取act_ru_task中assignee=zhangsan
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("zhangsan") //指定查询的条件
                .list();

        for (Task task : taskList) {
            System.out.println("task.getId() = " + task.getId());//2bd55616-834c-11f0-ba21-ae3fe006c356 8426c930-834d-11f0-a1d4-de0a7aaf3033
        }
    }

    /**
     * 任务审批
     */
    @Test
    void completeTask() {
        //第1条
//        taskService.complete("2bd55616-834c-11f0-ba21-ae3fe006c356");//人事审批
//        taskService.complete("d3224a02-834f-11f0-b314-82c4a4217fe7");//经理审批

        //第2条
//        taskService.complete("8426c930-834d-11f0-a1d4-de0a7aaf3033");//人事审批
        taskService.complete("7b5f22d6-8350-11f0-bd8c-82c4a4217fe7");//经理审批
    }

    /**
     * 挂起和激活流程定义
     */
    @Test
    void suspendedProcessDefinition() {
        //流程挂起和激活 --> 针对的流程定义
        String processDefinitionId = "FirstFlow:1:d2b617dd-834b-11f0-95a1-ae3fe006c356";
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();

        //判断当前流程定义已经被挂起
        if (processDefinition.isSuspended()) {
            //挂起 =》激活
            System.out.println("激活流程");
            repositoryService.activateProcessDefinitionById(processDefinitionId);
        }else {
            //激活 =》挂起
            System.out.println("挂起流程");
            repositoryService.suspendProcessDefinitionById(processDefinitionId);
        }
    }

    /**
     * 挂起和激活流程实例
     */
    @Test
    void suspendedProcessInstance() {
        //流程挂起和激活 --> 针对的流程定义
        String processInstanceId = "8cf9ae43-8354-11f0-9a54-12b9cd688963";
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();

        //判断当前流程定义已经被挂起
        if (processInstance.isSuspended()) {
            //挂起 =》激活
            System.out.println("激活流程");
            runtimeService.activateProcessInstanceById(processInstanceId);
        }else {
            //激活 =》挂起
            System.out.println("挂起流程");
            runtimeService.suspendProcessInstanceById(processInstanceId);
        }
    }

}
